package com.zmn.plat.dubbo.impl.part;

import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.common.dto.part.PartSkuDIO;
import com.zmn.plat.common.dto.part.PartSkuDRO;
import com.zmn.plat.dubbo.interfaces.part.PartSkuListRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.dto.part.PartSkuDTO;
import com.zmn.plat.model.entity.part.PartSkuQuery;
import com.zmn.plat.model.vo.part.PartSkuVO;
import com.zmn.plat.services.interfaces.part.PartSkuService;
import com.zmn.plat.utils.bean.BeanCopyUtils;
import org.apache.dubbo.config.annotation.Service;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cglib.beans.BeanCopier;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 类描述： BasePartRemoteServiceImpl
 *
 * @author DaiZhichao
 * @version v1.0
 * @since 2018/10/24 10:18
 */

@Service(loadbalance = "roundrobin", timeout = 5000, retries = -1, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class PartSkuListRemoteServiceImpl implements PartSkuListRemoteService {

    private Logger logger = LoggerFactory.getLogger(PartSkuListRemoteServiceImpl.class);
    @Resource
    PartSkuService partSkuService;

    private static final String TAG = "配件 dubbo 接口";

    /**
     * 描述:通过配件ID获取配件详情
     *
     * @param skuId Integer
     * @return ResponseDTO<PartSkuDRO>
     * @author DaiZhichao
     * @since 2018/10/31 11:18
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<PartSkuDRO> getBySkuId(Integer skuId) {
        if (Objects.isNull(skuId)) {
            logger.error(String.format("[%s]参数:[%s]不能为空", TAG, "skuId"));
            return ResponseDTO.fail("skuId不能为空");
        }
        PartSkuDTO partDTO = partSkuService.findPartDTOById(skuId);

        if (Objects.isNull(partDTO)) {
            logger.error(String.format("[%s]参数:[%s]对应的PartSku对象不存在", TAG, "skuId"));
            return ResponseDTO.fail(String.format("[%s]对应的PartSku对象不存在", skuId));
        }

        PartSkuDRO partSkuDRO = new PartSkuDRO();

        try {
            BeanCopyUtils.copyProperties(partDTO, partSkuDRO);
        } catch (Exception e) {
            logger.error(String.format("[%s]对象:[%s]拷贝出错,ex:[%s]", TAG, "CopyObjectUtil<PartSkuVO, PartSkuDRO>", e.getMessage()));
            return ResponseDTO.fail("拷贝PartSku对象出错");
        }

        return ResponseDTO.success(partSkuDRO, "OK");
    }

    /**
     * 描述:通过配件ID列表获取配件列表信息
     *
     * @param ids List<Integer>
     * @return ResponseDTO<PartSkuDRO>
     * @author DaiZhichao
     * @since 2018/10/31 11:20
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<PartSkuDRO>> listBySkuIds(List<Integer> ids) {
        if (Objects.isNull(ids) || ids.size() == 0) {
            logger.error(String.format("[%s]参数:[%s]不能为空或者长度为0", TAG, "ids"));
            return ResponseDTO.fail(String.format("[%s]参数:[%s]不能为空或者长度为0", TAG, "ids"));
        }

        List<PartSkuDTO> partSkuDTOList = partSkuService.listPartDTOByIds(ids);
        if (partSkuDTOList.isEmpty()) {
            logger.error(String.format("[%s]参数:[%s]不能为空或者长度为0", TAG, "ids"));
            return ResponseDTO.fail(String.format("[%s]对应的PartSku对象不存在", TAG));
        }
        ArrayList<PartSkuDRO> partSkuDROList = new ArrayList<>();
        try {
            BeanCopier copier = BeanCopier.create(PartSkuDTO.class, PartSkuDRO.class, false);
            partSkuDTOList.forEach(partSkuDTO -> {
                PartSkuDRO partSkuDRO = new PartSkuDRO();
                copier.copy(partSkuDTO, partSkuDRO, null);
                partSkuDROList.add(partSkuDRO);
            });
        } catch (Exception e) {
            logger.error(String.format("[%s]对象:[%s]拷贝出错,ex:[%s]", TAG, "CopyObjectUtil<PartSkuDTO, PartSkuDRO>", e.getMessage()));
            return ResponseDTO.fail("拷贝PartSkuDTO对象出错");
        }
        return ResponseDTO.success(partSkuDROList, "OK");
    }

    /**
     * 描述:通过PartSkuDIO查询配件信息
     *
     * @param partSkuDIO PartSkuDIO
     * @return ResponseDTO<PartSkuDRO> msg = count
     * @author DaiZhichao
     * @since 2018/10/31 11:20
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<PartSkuDRO>> listPageByQuery(PartSkuDIO partSkuDIO) {

        logger.info("ES配件查询====================================");
        List<PartSkuDRO> partSkuDROList;
        Integer count;
        try {
            PartSkuQuery query = Objects.isNull(partSkuDIO) ? new PartSkuQuery() : this.partSkuDIOToQuery(partSkuDIO);
            List<PartSkuVO> partSkuVOList = partSkuService.listVOPageByQuery(query);
            partSkuDROList = partSkuVOList.stream().map(this::partSkuVOToDRO).collect(Collectors.toList());
            count = partSkuService.countByQuery(query);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ResponseDTO.fail("获取配件列表失败,请重试");
        }

        return ResponseDTO.success(partSkuDROList, count.toString());
    }

    /**
     * 描述:通过PartSkuDIO在ESz中模糊查询配件简单信息
     *
     * @param partSkuDIO PartSkuDIO
     * @return ResponseDTO<PartSkuDRO> msg = count
     * @author DaiZhichao
     * @since 2018/10/31 11:20
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<PartSkuDRO>> listPageByEsSearch(@NotNull PartSkuDIO partSkuDIO) {

        return ResponseDTO.fail("获取配件列表失败");
    }

    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<PartSkuDRO>> listByCategoryId(Integer categoryId) {
        if (categoryId == null) {
            return ResponseDTO.fail("传入的CategoryId不能为NULL");
        }
        List<PartSkuDRO> partSkuDROList;
        try {
            PartSkuQuery query = new PartSkuQuery();
            query.setCategId(categoryId);
            List<PartSkuVO> partSkuVOList = partSkuService.listVOPageByQuery(query);
            partSkuDROList = partSkuVOList.stream().map(this::partSkuVOToDRO).collect(Collectors.toList());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ResponseDTO.fail("拷贝PartSku对象出错");
        }

        return ResponseDTO.success(partSkuDROList, "OK");
    }

    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<VtDTO>> listVtDTOByCategoryId(Integer categoryId) {
        if (categoryId == null) {
            return ResponseDTO.fail("传入的CategoryId不能为NULL");
        }
        PartSkuDIO partSkuDIO = new PartSkuDIO();
        partSkuDIO.setCategId(categoryId);
        return queryVtDTOList(partSkuDIO);
    }

    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<VtDTO>> listByText(String text) {
        if (StringUtil.isBlank(text)) {
            return ResponseDTO.fail("text参数不能为空");
        }
        try {
            List<VtDTO> vtDTOS = partSkuService.listByText(text);
            return ResponseDTO.success(vtDTOS, "OK");
        } catch (Exception e) {
            logger.error("{} error：{}", TAG, e.getMessage(), e);
            return ResponseDTO.fail(e.getMessage());
        }
    }

    /**
     * 描述:通过PartSkuDIO获取配件简单列表
     *
     * @param skuDIO PartSkuDIO
     * @return ResponseDTO<VtDTO>
     * @author DaiZhichao
     * @since 2018/10/31 11:20
     */
    @ReadOnlyConnection
    private ResponseDTO<List<VtDTO>> queryVtDTOList(PartSkuDIO skuDIO) {
        if (skuDIO == null) {
            logger.error(String.format("[%s]参数:[%s]不能为空", TAG, "skuDIO"));
            return ResponseDTO.fail("query不能为空");
        }

        PartSkuQuery skuQuery = new PartSkuQuery();

        try {
            BeanCopyUtils.copyProperties(skuDIO, skuQuery);
        } catch (Exception ex) {
            logger.error(String.format("[%s]对象:[%s]拷贝出错,ex:[%s]", TAG, "CopyObjectUtil<PartSkuVO, PartSkuDRO>", ex.getMessage()));
            return ResponseDTO.fail("拷贝PartSkuQuery对象出错");
        }

        List<VtDTO> partSkuVOList = partSkuService.listVtDTOByQuery(skuQuery);

        if (partSkuVOList.isEmpty()) {
            logger.error(String.format("[%s]Query对应的PartSku对象不存在", TAG));
            return ResponseDTO.fail("Query对应的PartSku对象不存在");
        }
        return ResponseDTO.success(partSkuVOList, "OK");
    }

    private PartSkuDRO partSkuVOToDRO(PartSkuVO vo) {
        PartSkuDRO dro = new PartSkuDRO();
        BeanCopyUtils.copyProperties(vo, dro);
        return dro;
    }

    /**
     * 描述:将DIO转换为Query以便查询 ES增加查询参数时需要更更改此方法
     *
     * @param dio PartSkuDIO
     * @return ResponseDTO<VtDTO>
     * @author DaiZhichao
     * @since 2019/02/18 11:20
     */
    private PartSkuQuery partSkuDIOToQuery(PartSkuDIO dio) {
        PartSkuQuery query = new PartSkuQuery();
        query.setCategId(dio.getCategId());
        query.setCategOneId(dio.getCategOneId());
        query.setName(dio.getName());
        query.setStatus(dio.getStatus());
        query.setPageNum(dio.getPageNum());
        query.setPageSize(dio.getPageSize());
        return query;
    }
}
